//
//  GerenxinxiViewController.m
//  GuoBang
//
//  Created by fengshuai on 16/4/28.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "GerenxinxiViewController.h"
#import "GrxxTableViewCell.h"
#import "GeRenDetailsViewController.h"
#import "GeRenDetailsViewController.h"
#import "MyPickerSelectView.h"
#import "DiZhiViewController.h"

#define NAVHEIGHT CGRectGetMaxY(self.navigationController.navigationBar.frame)
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define RGBCOLOR(r, g, b)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define SCREEN_WIDTH    ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT   ([[UIScreen mainScreen] bounds].size.height)

@interface GerenxinxiViewController ()<UITableViewDataSource, UITableViewDelegate,sendBackSelegate>
{
    GrxxTableViewCell *GrxxCell;
    MyPickerSelectView *myStlyPickerView;
    NSString *fileString;
    NSInteger selectFlag;
    
}

@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UITableView *setTableView;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSMutableArray *detailsArr;

@property (nonatomic, strong) NSArray *sexArr;
@property (nonatomic, strong) NSArray *workArr;
@property (nonatomic, strong) NSArray *ageArr;


@end

@implementation GerenxinxiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNVA];
    [self setupUI];
    [self setupData];
}

- (void)setupNVA
{
    self.title=@"个人信息";
    //设置返回按键
    //返回按键颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    //返回按键图标
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
}

- (void)leftAction:(UIBarButtonItem *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)setupData
{
    self.titleArr = @[@"名称", @"绑定手机", @"常用地址", @"性别", @"年龄", @"行业"];
    self.detailsArr = [NSMutableArray array];
    [self.detailsArr addObject:@""];
    [self.detailsArr addObject:@""];
    [self.detailsArr addObject:@""];
    [self.detailsArr addObject:@""];
    [self.detailsArr addObject:@""];
    [self.detailsArr addObject:@""];

    self.sexArr = @[@"请选择性别",@"男",@"女"];
    self.workArr = @[@"请选择行业",@"信息技术",@"金融保险",@"商业服务",@"其他"];
    self.ageArr = @[@"请选择年龄",@"60后",@"70后",@"80后",@"90后",@"00后",@"11后"];
    
}

- (void)setupUI
{
    
    self.setTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) style:UITableViewStyleGrouped];
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    [self.setTableView setScrollEnabled:NO];
    [self.view addSubview:self.setTableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(bring:) name:@"BringNotification" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(remove:) name:@"RemoveCoverView" object:nil];
    
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)bring:(NSNotification *)not
{
    [self.coverView removeFromSuperview];
    [myStlyPickerView removeFromSuperview];
    NSLog(@"%@",not.userInfo);
    fileString = [not.userInfo valueForKey:@"1"];
    selectFlag = [[not.userInfo valueForKey:@"2"] integerValue];
    [self.detailsArr replaceObjectAtIndex:selectFlag withObject:fileString];
    [self.setTableView reloadData];
 
}



- (void)remove:(NSNotification *)not
{
    [self.coverView removeFromSuperview];
}






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customcell"];
    
    
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GrxxTableViewCell" owner:self options:nil];
    if (nib.count > 0) {
        GrxxCell = [nib objectAtIndex:0];
       
        if (indexPath.section == 0) {
           GrxxCell.titileLabel.text = self.titleArr[indexPath.row];
            GrxxCell.detailsLabel.tag = indexPath.row;
            GrxxCell.detailsLabel.text = self.detailsArr[indexPath.row];
        }else if (indexPath.section == 1) {
            GrxxCell.titileLabel.text = self.titleArr[indexPath.row + 3];
            GrxxCell.detailsLabel.tag = indexPath.row + 3;
            GrxxCell.detailsLabel.text = self.detailsArr[indexPath.row + 3];
        }
       

        
        cell = GrxxCell;
    }
    
    
    
    
    
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                GeRenDetailsViewController *geRenDVC = [GeRenDetailsViewController new];
                geRenDVC.mytitle = @"姓名";
                geRenDVC.flag = 0;
                geRenDVC.delegate = self;
                [self.navigationController pushViewController:geRenDVC animated:YES];
              
            }
                break;
            case 1:
            {
                GeRenDetailsViewController *geRenDVC = [GeRenDetailsViewController new];
                geRenDVC.mytitle = @"手机";
                 geRenDVC.flag = 1;
                 geRenDVC.delegate = self;
                [self.navigationController pushViewController:geRenDVC animated:YES];
            }
                break;
            case 2:
            {
                DiZhiViewController *dizhiVC = [DiZhiViewController new];
                [self.navigationController pushViewController:dizhiVC animated:YES];
            }
                break;
            default:
                break;
        }
    }else {
        switch (indexPath.row) {
            case 0:
            {
                [self selectButtonAction:3];
            }
                NSLog(@"111111111");
                break;
            case 1:
            {

                
                [self selectButtonAction:4];
                
                
            }
                break;
            case 2:
            {
                
                [self selectButtonAction:5];
            }
                break;
            default:
                break;
        }
        
    }
}



-(void)sendbackStr:(NSString *)str flag:(NSInteger)flag
{
    
    NSLog(@"传过来了吗 %@ %ld",str,flag);
    [self.detailsArr replaceObjectAtIndex:flag withObject:str];
    
    NSLog(@"%@",self.detailsArr);
    
    [self.setTableView reloadData];
  
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)rightAction
{
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}



// 点击低下弹出自定义的View的方法
- (void)selectButtonAction:(NSInteger)sender
{
    [self.view endEditing:YES];
  
    [myStlyPickerView removeFromSuperview];
    
    
    if (sender == 3) {
        myStlyPickerView = [[MyPickerSelectView alloc] initWithFrame:CGRectMake(0, HEIGHT - 250, WIDTH, 250) withArray:self.sexArr withFlag:sender];
        self.coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 250)];
        self.coverView.backgroundColor = [UIColor blackColor];
        self.coverView.alpha = 0.4;
        [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
        [self.view addSubview:myStlyPickerView];
    }else if (sender == 4) {
        
        myStlyPickerView = [[MyPickerSelectView alloc] initWithFrame:CGRectMake(0, HEIGHT - 250, WIDTH, 250) withArray:self.ageArr withFlag:sender];
        self.coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 250)];
        self.coverView.backgroundColor = [UIColor blackColor];
        self.coverView.alpha = 0.4;
        [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
        [self.view addSubview:myStlyPickerView];

        
        
        
    }else{
        myStlyPickerView = [[MyPickerSelectView alloc] initWithFrame:CGRectMake(0, HEIGHT - 250, WIDTH, 250) withArray:self.workArr withFlag:sender];
        self.coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 250)];
        self.coverView.backgroundColor = [UIColor blackColor];
        self.coverView.alpha = 0.4;
        [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
        [self.view addSubview:myStlyPickerView];
        
    }
    

    
}











@end
